package ext.tianma.part.forms;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFCell;

import wt.part.WTPart;
import wt.util.WTException;
import ext.com.core.CoreUtil;
import ext.com.iba.IBAUtil;
import ext.com.workflow.WorkflowUtil;

public class PartUtil 
{
	private static final String PART_SUPPLIER_NAME	 = "partSupplierWithSystemName";
	
	public static String getRawMaterialsUnit(Sheet sheet,String EName) throws WTException
	{
		//XSSFSheet sheet = we.getSheetAt(0);
		String code =null;
		for (int i = 1; i < sheet.getLastRowNum(); i++) {
			Row row = sheet.getRow(i+1);
			String temp = getCellValue(row.getCell(1));
			//System.out.println("temp:"+temp);
			if(null==temp||temp.equals(""))
			{
				continue;
			}
			else
			{
				if(temp.equals(EName))
				{
					code = getCellValue(row.getCell(6));	
					break;
				}
			}
		}
		return code;
	}
	
	public static String getLitteClassCode(Sheet sheet,String EName) throws WTException
	{
		//XSSFSheet sheet = we.getSheetAt(0);
		String code =null;
		for (int i = 1; i < sheet.getLastRowNum(); i++) {
			Row row = sheet.getRow(i+1);
			String temp = getCellValue(row.getCell(1));
			//System.out.println("temp:"+temp);
			if(null==temp||temp.equals(""))
			{
				continue;
			}
			else
			{
				if(temp.equals(EName))
				{
				String	s = getCellValue(row.getCell(1));
				String  s1=getCellValue(row.getCell(3));
				String  s2=getCellValue(row.getCell(4));
				String  s3=getCellValue(row.getCell(5));
				System.out.println("s="+s+" | s1="+s1+" | s2="+s2+" | s3="+s3);
				code=s+","+s1+","+s2+","+s3;
				break;
				}
			}
		}
		return code;
	}
	
	public static String getTwoMaterialName(Sheet sheet,String name) throws WTException
	{
		String code=null;
		for (int i = 1; i < sheet.getLastRowNum(); i++) {
			Row row = sheet.getRow(i);
			String temp = getCellValue(row.getCell(1));
			//System.out.println("temp:"+temp);
			if(null==temp||temp.equals(""))
			{
				continue;
			}
			else
			{
				if(temp.equals(name))
				{
				String s1=getCellValue(row.getCell(0));
				String	s = getCellValue(row.getCell(2));
			
				code=s+","+s1;
					System.out.println(">>>>>>>>>>>>>>>> code="+code);
				}
			}
		}
		
		return code;
	}
	/**
	 * 获取单元格中的信息
	 * 
	 * @param cell
	 *            单元格
	 * @return 主要参数信息
	 * @throws Exception
	 */
	public static String getCellValue(Cell cell) throws WTException {
		String value = "";
		try {
			if (null == cell) {
				return "";
			}
			switch (cell.getCellType()) {
			case XSSFCell.CELL_TYPE_NUMERIC:
				value = Double.toString(cell.getNumericCellValue());
				break;
			case XSSFCell.CELL_TYPE_STRING:
				value = cell.getStringCellValue();
				break;
			case XSSFCell.CELL_TYPE_BLANK:
				value = "";
				break;
			case XSSFCell.CELL_TYPE_BOOLEAN:
				value = Boolean.toString(cell.getBooleanCellValue());
				break;
			case XSSFCell.CELL_TYPE_FORMULA:
				try {
					value = String.valueOf(cell.getNumericCellValue());
				} catch (Exception e) {
					value = cell.getStringCellValue();
					// TODO: handle exception
				}
				break;
			default:
				break;
			}
		} catch (Exception e) {
		}
		// System.out.println("excel 单元格的值："+value);
		if (value.indexOf(".") != -1) {
			value = value.substring(0, value.indexOf("."));
		}
		return value;
	}
	
	/**
	 * 再启动二供的时候ERP供应商需要清空
	 * @param part
	 */
	public static void closePartAttributes( WTPart part ){
		
		if( part == null ){
			return;
		}
		
		try {
			if( !WorkflowUtil.isObjectCheckedOut( part ) ){
				part = ( WTPart ) CoreUtil.checkoutObject( part );
			}
			
			part = (WTPart) IBAUtil.forceSetIBAValue( part , PART_SUPPLIER_NAME , "" );
			
			CoreUtil.checkinObject(part, "一供清除！");
			
		} catch (WTException e) {
			e.printStackTrace();
		}
		
	}
}
